package com.software.backend.login.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王俊坤
 * @version 1.0
 * @date 2022/11/10 16:31
 */

/**
 * 规范化处理结果的类
 * @param <T>
 *      code 结果码
 *      msg  结果描述信息
 *      data 结果的数据集
 */
public class Result<T> {

    private int code;
    private String msg;
    private List<T> data;

    public Result(int code, String msg, List<T> data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    /**
     * @param data 需要被封装的数据源List
     * @return 返回生成好的Result对象
     */
    public static Result getSuccessResult(List data){ return new Result(200, "成功", data);}
    public static Result getErrorResult(List data){ return new Result(500, "失败，请重试", data);}
    public static Result getSuccessResult(Object data){
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(data);
        return new Result(200, "成功", objects);
    }
    public static Result getErrorResult(Object data){
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(data);
        return new Result(500, "失败，请重试", objects);
    }

    /**
     * @param
     *      code 返回结果码;
     *      msg 返回的结果信息;
     *      data 需要被封装的数据源List
     * @return 返回生成好的Result对象
     */
    public static Result getSuccessResult(int code, String msg, List data){ return new Result(code, msg, data);}
    public static Result getErrorResult(int code, String msg, List data){ return new Result(code, msg, data);}
    public static Result getSuccessResult(int code, String msg, Object data){
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(data);
        return new Result(200, "成功", objects);
    }
    public static Result getErrorResult(int code, String msg, Object data){
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(data);
        return new Result(500, "失败，请重试", objects);
    }
}
